//
//  ChangePhoneViewController.m
//  NOAHWM
//
//  Created by Frank on 6/13/13.
//  Copyright (c) 2013 Ryan. All rights reserved.
//

#import "ChangePhoneViewController.h"
#import "NSString+MobileNum.h"
#import "NYMemberCenterDataManager.h"
#import "JSON.h"


@interface ChangePhoneViewController ()

@end

@implementation ChangePhoneViewController

@synthesize phoneTextField;

#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(enableSendButton) object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)saveButtonPressed:(id)sender {
    NSString *message = self.phoneTextField.text;
    if ([NSString isMobileNumber:message]==NO) {
        message = [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000003E"],@"手机号"];
        if(self.phoneTextField.text.length < 1)
            message = [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000001E"],@"手机号"];
        else if(self.phoneTextField.text.length > 11)
            message = [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000002E"],@"手机号"];
        [[RYHUDManager sharedManager] showWithMessage:message customView:nil hideDelay:2.f];
        return;
    }
    else{
        [self.phoneTextField resignFirstResponder];
        NSString *suburl = [NSString stringWithFormat:kNewPhoneCommitUrl,@"jsonp",[NYMemberCenterDataManager sharedManager].loginMember.accountId,message,@""];
        suburl = [suburl stringByAppendingString:@"app000019"];
        NSString *url = [NSString stringWithFormat:@"%@%@",kServerAddress,suburl];
        url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:url delegate:self purpose:nil];
        [[RYHUDManager sharedManager] startedNetWorkActivityWithText:@"手机号提交中..."];
    }
}
#pragma mark -

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviTitle: @"手机"];
    [self setLeftNaviItemWithTitle:@"返回" imageName:@"Icon_Back"];
    [self setRightNaviItemWithTitle:nil imageName:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    if (self.isViewLoaded&&!self.view.window) {
        self.phoneTextField = nil;
    }// Dispose of any resources that can be recreated.
}

- (void)dealloc{
    [phoneTextField release];
    [super dealloc];
}

#pragma mark - RYDownloaderDelegate methods
- (void)downloader:(RYDownloader*)downloader completeWithNSData:(NSData*)data
{
    NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //去掉被包的jsonp字串
    if(str.length > 7)
        str = [str substringWithRange:NSMakeRange(6, str.length-7)];
    NSDictionary *dict = [str JSONValue];
    if([[dict objectForKey:@"result"] isEqualToString:@"1"])
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    [[RYHUDManager sharedManager] showWithMessage:[dict objectForKey:@"message"] customView:nil hideDelay:2.f];
}

- (void)downloader:(RYDownloader*)downloader didFinishWithError:(NSString*)message
{
    [[RYHUDManager sharedManager] showWithMessage:kNetWorkErrorString customView:nil hideDelay:2.f];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
